﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using OMS.OMSService;

namespace OMS.Models
{
    public class OrderModelView
    {
        public OrderModelView() {
            OrderItems = new List<OrderItemModel>();
        }

        public OrderModelView(OrderModel order)
        {
            OrderNumber = order.OrderNumber;
            Id = order.Id;
            TotalPrice = order.TotalPrice;
            Status = order.Status;
            DateOfOrdering = order.DateOfOrdering;
            PrefDate = order.PrefDeliveryDate;
            DeliveryDate = order.DeliveryDate;
            OrderItems = new List<OrderItemModel>();
            if (order.MerchId != null)
            {
                Asignee = (int)order.MerchId;
            }
            foreach (OrderItemModel orderItemModel in order.OrderItems)
            {
                OrderItems.Add(orderItemModel);
            }
        }

        public OrderModel ToOrderModel()
        {
            OrderModel orderModel = new OrderModel
            {
                Id = this.Id,                
                TotalPrice = this.TotalPrice,
                Status = this.Status,
                DateOfOrdering = this.DateOfOrdering,
                PrefDeliveryDate = this.PrefDate,
                DeliveryDate = this.DeliveryDate,
                OrderItems = this.OrderItems.ToArray(),
                MerchId = Asignee
            };
            return orderModel;
        }

        public int Id { get; set; }

        public List<OrderItemModel> OrderItems { get; set; }

        [Required]
        [StringLength(8, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)]
        [DataType(DataType.Text)]
        [Display(Name = "Quantity")]
        public string OrderNumber { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Status")]
        public string Status { get; set; }

        [DataType(DataType.Currency)]
        [Display(Name = "Total Price")]
        public decimal TotalPrice { get; set; }

        [Display(Name = "Number of Items")]
        public decimal NumberOfItems { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Date of Ordering")]
        public DateTime DateOfOrdering { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Preferable Delivery Date")]
        public DateTime PrefDate { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Delivery Date")]
        public DateTime DeliveryDate { get; set; }

        public int Asignee { get; set; }

        [Display(Name = "Asignee")]
        public string AsigneeName { get; set; }
    }
}